package com.sykj.why.controller.admin.menu;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.MenuT;
import com.sykj.why.mongo.dto.PageDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.MenuTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/4
 * @desc 后台菜单管理控制访问层
 */

@RestController
@RequestMapping("/admin/menu")
@Slf4j
@Api(value = "/admin/menu",description = "后台菜单管理控制访问层")
public class MenuController {

    @Autowired
    private MenuTService menuTService;

    /**
     * 添加菜单
     * @param menuT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加菜单")
    public ResponseEntity add(@RequestBody MenuT menuT){
        try {
            menuTService.saveMenu(menuT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove",notes = "删除菜单")
    public ResponseEntity remove(@PathVariable String id){
        try {
            menuTService.removeMenu(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改菜单
     * @param menuT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改菜单")
    public ResponseEntity update(@RequestBody MenuT menuT){
        try {
            menuTService.updateMenu(menuT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 启用/禁用菜单
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    @ApiOperation(value = "/enable",notes = "启用/禁用菜单")
    public ResponseEntity enable(@RequestParam String id,@RequestParam boolean flag){
        try {
            menuTService.disableOrEnable(id, flag);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("启用禁用菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询全部
     *
     * @return 全部菜单
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value ="/findAll",notes = "查询全部菜单")
    public ResponseEntity findAll() {
        try {
            return Response.success("查询成功", menuTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 分页查询菜单
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    @ApiOperation(value = "/findByPage",notes = "分页查询菜单")
    public ResponseEntity findByPage(@RequestBody PageDTO pageDTO){
        try {
            return Response.success("查询成功", menuTService.findByPage(pageDTO));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("分页查询菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 通过栏目查询该栏目下的菜单树状图
     * @param programaId
     * @return
     */
    @RequestMapping(value = "/findMenuByPrograma/{programaId}",method = RequestMethod.GET)
    @ApiOperation(value = "/findMenuByPrograma/{programaId}",notes = "通过栏目查询该栏目下的菜单树状图")
    public ResponseEntity findMenuByPrograma(@PathVariable String programaId){
        try {
            return Response.success("查询成功",menuTService.findMenuTreeByProgramaIdAndPid(programaId,"0"));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("条件查询菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
